//
//  LLAnimationAlertView.m
//  Receipt
//
//  Created by 刘玲玉 on 2019/5/13.
//  Copyright © 2019 LG. All rights reserved.
//

#import "LLAnimationAlertView.h"

static CGFloat duration = 0.3;

@interface LLAnimationAlertView ()

@property (nonatomic, assign) AnimationType animationType;
@property (nonatomic, assign) BOOL tapDismiss;

@end

@implementation LLAnimationAlertView

+ (instancetype)animationAlertViewWithView:(UIView *)alertView animationType:(AnimationType)animationType
{
    return [[self alloc] initWithFrame:[UIScreen mainScreen].bounds alertView:alertView animationType:animationType tapBgDismiss:YES inView:nil];
}

+ (instancetype)animationAlertViewWithView:(UIView *)alertView animationType:(AnimationType)animationType tapBgDismiss:(BOOL)tapDismiss
{
    return [[self alloc] initWithFrame:[UIScreen mainScreen].bounds alertView:alertView animationType:animationType tapBgDismiss:tapDismiss inView:nil];
}

///点击空白处能不能消失
+ (instancetype)animationAlertViewWithView:(UIView *)alertView animationType:(AnimationType)animationType tapBgDismiss:(BOOL)tapDismiss inView:(nullable UIView *)fuView
{
    return [[self alloc] initWithFrame:[UIScreen mainScreen].bounds alertView:alertView animationType:animationType tapBgDismiss:tapDismiss inView:fuView];
}

- (instancetype)initWithFrame:(CGRect)frame alertView:(UIView *)alertView animationType:(AnimationType)animationType tapBgDismiss:(BOOL)tapDismiss inView:(UIView *)fuView
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.tapDismiss = tapDismiss;
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
        _alertView = alertView;
        
        [self addSubview:alertView];
        
        self.animationType = animationType;
        
        objc_setAssociatedObject(alertView, @selector(dismissView:), @(animationType), OBJC_ASSOCIATION_ASSIGN);
        
        if (fuView == nil) {
            [[UIApplication sharedApplication].keyWindow addSubview:self];
        } else {
            [fuView addSubview:self];
        }
        
    }
    
    return self;
}

- (void)setAnimationType:(AnimationType)animationType
{
    _animationType = animationType;
    
    if (animationType == AnimationFromTop) {
        
        self.alertView.top = -self.alertView.height;
        self.alertView.centerX = self.centerX;
        [UIView animateWithDuration:duration animations:^{
            self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
            self.alertView.top = 0;
        }];
        
    } else if (animationType == AnimationFromBottom) {
        
        self.alertView.top = self.bottom;
        self.alertView.centerX = self.centerX;
        [UIView animateWithDuration:duration animations:^{
            self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
            self.alertView.bottom = self.bottom;// - Tabbar_Bottom_height;
        }];
        
    } else if (animationType == AnimationFromLeft) {
        
        self.alertView.left = -self.alertView.width;
        [UIView animateWithDuration:duration animations:^{
            self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
            self.alertView.left = 0;
        }];
        
    } else if (animationType == AnimationFromRight) {
        
        self.alertView.left = self.width;
        [UIView animateWithDuration:duration animations:^{
            self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
            self.alertView.right = self.right;
        }];
        
    } else if (animationType == AnimationBottomToCenter) {
        self.alertView.centerX = self.centerX;
        self.alertView.top = self.bottom;
        [UIView animateWithDuration:duration animations:^{
            self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
            self.alertView.center = self.center;
        }];
    }
}

+ (void)dismissView:(UIView *)alertView
{
    NSNumber *animationTypeNum = objc_getAssociatedObject(alertView, _cmd);
    AnimationType animationType = animationTypeNum.integerValue;
    
    UIView *selfView = alertView.superview;
    
    if (selfView && [selfView isKindOfClass:[LLAnimationAlertView class]]) {
        
        [UIView animateWithDuration:duration animations:^{
            
            selfView.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
            
            if (animationType == AnimationFromTop) {
                
                alertView.top = -alertView.height;
                
            } else if (animationType == AnimationFromBottom) {
                
                alertView.top = selfView.bottom;
                
            } else if (animationType == AnimationFromLeft) {
                
                alertView.left = -alertView.width;
                
            } else if (animationType == AnimationFromRight) {
                
                alertView.left = selfView.width;
                
            } else if (animationType == AnimationBottomToCenter) {
                alertView.bottom = selfView.top;
            }
            
        } completion:^(BOOL finished) {
            
            [selfView removeFromSuperview];
            
        }];
    }
    
}

- (void)dismissView
{
    [UIView animateWithDuration:duration animations:^{
        
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
        
        if (self.animationType == AnimationFromTop) {
            
            self.alertView.top = -self.alertView.height;
            
        } else if (self.animationType == AnimationFromBottom) {
            
            self.alertView.top = self.bottom;
            
        } else if (self.animationType == AnimationFromLeft) {
            
            self.alertView.left = -self.alertView.width;
            
        } else if (self.animationType == AnimationFromRight) {
            
            self.alertView.left = self.width;
            
        } else if (self.animationType == AnimationBottomToCenter) {
            self.alertView.bottom = self.top;
        }
        
    } completion:^(BOOL finished) {
        
        [self removeFromSuperview];
        
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    if (!self.tapDismiss) {
        return;
    }
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    if (!CGRectContainsPoint(self.alertView.frame, point)) {
        [self dismissView];
        
        if (self.dismissBlock) {
            self.dismissBlock();
        }
    }
}

- (void)dealloc
{
    NSLog(@"--> LLAnimationAlertView <-- 释放了");
}

@end
